package com.stylefeng.guns.modular.vpn_channel_info.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.model.VpnChannelInfo;
import com.stylefeng.guns.modular.system.dao.VpnChannelInfoMapper;
import com.stylefeng.guns.modular.vpn.dto.ChannelDTO;
import com.stylefeng.guns.modular.vpn.entity.ChannelDO;
import com.stylefeng.guns.modular.vpn.utils.GenCodeUtil;
import com.stylefeng.guns.modular.vpn.vo.Paging;
import com.stylefeng.guns.modular.vpn_channel_info.service.IVpnChannelInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-06-05
 */
@Service
public class VpnChannelInfoServiceImpl extends ServiceImpl<VpnChannelInfoMapper, VpnChannelInfo> implements IVpnChannelInfoService {


    @Value("${channel_promote-url}")
    private String chanelPromoteUrl;

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    @Transactional
    public void addChannel(String channelName) {
        if (StringUtils.isEmpty(channelName)){
            throw new GunsException(BizExceptionEnum.VPN_CHANNEL_NAME_ERROR);
        }

        try {
            Integer count =  this.baseMapper.maxChannelId();
            if (count == null){
                count = 0;
            }
            String channelNo = GenCodeUtil.toSerialCode(count+1).toUpperCase();
            VpnChannelInfo channelInfo = new VpnChannelInfo();
            channelInfo.setName(channelName);
            channelInfo.setChannelNo(channelNo);
            channelInfo.setCreateTime(new Date());
            channelInfo.setStatus(1);
            channelInfo.setChannelPromoteUrl(chanelPromoteUrl+"?channelNo="+channelNo);
            this.baseMapper.insert(channelInfo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new GunsException(BizExceptionEnum.VPN_CHANNEL_INSERT_ERROR);
        }
    }

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(ChannelDTO channelDTO) {
        Paging<ChannelDO> paging = new Paging<>();
        EntityWrapper<VpnChannelInfo> wrapper = new EntityWrapper<>();
        if (!StringUtils.isEmpty(channelDTO.getBeginTime())){
            wrapper.ge("create_time",channelDTO.getBeginTime());
        }
        if (!StringUtils.isEmpty(channelDTO.getEndTime())){
            wrapper.le("create_time",channelDTO.getEndTime());
        }
        if (!StringUtils.isEmpty(channelDTO.getChannelStatus())){
            wrapper.eq("status",channelDTO.getChannelStatus());
        }
        if (!StringUtils.isEmpty(channelDTO.getChannelName())){
            wrapper.like("name",channelDTO.getChannelName());
        }

        Integer count = this.baseMapper.selectCount(wrapper);
        paging.setTotal(count.longValue());

        int start = channelDTO.getPage() > 0 ? channelDTO.getPage() - 1 : 0;
        channelDTO.setStart(start * channelDTO.getLimit());
        channelDTO.setCount(channelDTO.getLimit());
        List<ChannelDO> channelActionDOList = this.baseMapper.getList(channelDTO);
        paging.setItems(channelActionDOList);
        return paging;
    }

    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void updateChannelStatus(Integer channelId, Integer status) {
        VpnChannelInfo vpnChannelInfo = this.baseMapper.selectById(channelId);
        if (vpnChannelInfo == null){
            throw new GunsException(BizExceptionEnum.VPN_CHANNEL_NOT_EXIST);
        }

        vpnChannelInfo.setStatus(status);
        vpnChannelInfo.setEndTime(new Date());
        this.baseMapper.updateById(vpnChannelInfo);
    }
}
